﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Beetle;

namespace BugVisual.Controllers
{
    public class MishaBugController : Controller
    {
            static Target[] targets = new Target[]{
            new Target(new Position(20, 100), 1),
            new Target(new Position(580, 160), -1)
            };
            static Bettle bug = new Bettle("fandorin", "SuperNeuro.xml", "errors.txt");
            //static Bettle bug = new Bettle("fandorin");
            public JsonResult NewTarget(int X, int Y, bool IsFood)
            {
                int i;
                if (IsFood) i = 0;
                else i = 1;
                targets[i].coord.x = X;
                targets[i].coord.y = Y;
                return Json(new { }, JsonRequestBehavior.AllowGet);
            }
            public JsonResult NextIteration()
            {
                int counter =10;
                while (counter > 0)
                {
                    bug.MakeStepWithLearning(targets);
                    counter--;
                }

                // Bettle bug = new Bettle();
                List<object> legs = new List<object>();
                for (int i = 0; i < bug.Legs.Length; i++)
                {
                    int angle = 0;
                    if (bug.Legs[i].State == 0) angle = -15;
                    if (bug.Legs[i].State == 1) angle = 0;
                    if (bug.Legs[i].State == 2) angle = 15;
                    legs.Add(new
                    {
                        IsUp = (bug.Legs[i].IsUp == 1) ? true : false,
                        angle = angle,
                        legId = i + 1
                    });
                }
                return Json(new
                {
                    x = bug.position.x,
                    y = bug.position.y,
                    angle = bug.position.ViewAngle,
                    legs = legs
                }, JsonRequestBehavior.AllowGet);
            }

            public class Model
            {
                public Position food { get; set; }
                public Position danger { get; set; }
                public Position bug { get; set; }
            }


            public ActionResult Index()
            {
                //int counter = 10000;
                //while (counter > 0)
                //{
                //    bug.MakeStepWithLearning(targets);
                //    counter--;
                //}
                var pm = new Model();
                pm.food = targets[0].coord;
                pm.danger = targets[1].coord;
                pm.bug = bug.position;
                return View(pm);
            }

            public class pmErrors
            {
                public string[] labels { get; set; }
                public double[] data { get; set; }
            }
            public JsonResult GetErrors()
            {
                bug.CaclcError();

                var result = bug.ErrorList().ToArray();

                var pm = new pmErrors();


                pm.data = result;

                if (result.Count() > 500)
                {
                    List<double> tempData = new List<double>();
                    int koef = (result.Count() / 500) + 1;


                    int counter = 0;
                    while (counter < result.Count())
                    {
                        tempData.Add(result[counter]);
                        counter += koef;
                    }

                    pm.data = tempData.ToArray();
                }
                string[] temp = new string[pm.data.Count()];

                for (int i = 0; i < temp.Count(); i++) temp[i] = "";

                pm.labels = temp;

                return Json(pm, JsonRequestBehavior.AllowGet);
            }

            public JsonResult ResetPosition()
            {
                bug.position = new Position(300, 300) { ViewAngle = 15 };

                return Json(1, JsonRequestBehavior.AllowGet);
            }
            public JsonResult SaveNeuro()
            {
                ((Beetle.FandorinsBugBrain.FBrain)(bug.Brain)).neuro.Save("SuperNeuroFromSite.xml");

                return Json(1, JsonRequestBehavior.AllowGet);
            }
    }
}
